home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8838 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  82 lines

  1. Path: fnnews.fnal.gov!kriol
  2. From: kriol@fnal.gov (Oleg Krivosheev)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Will it be auto-deleted?
  5. Date: 27 Feb 1996 00:03:13 GMT
  6. Organization: FERMILAB, Batavia, IL
  7. Sender: kriol@martian (Oleg Krivosheev)
  8. Message-ID: <4gtho1$l4e@fnnews.fnal.gov>
  9. References: <1996Feb20.110314.46035@yogi.urz.unibas.ch> <4gg91j$gdc@clarknet.clark.net> <4gsff2$21k@news.us.net>
  10. NNTP-Posting-Host: martian.fnal.gov
  11. To: thoth256@us.net (Evelio Perez-Albuerne)
  12.  
  13. Hi,
  14.  
  15. |> gusty@clark.net (Harlan Messinger) wrote:
  16. |> 
  17. |> >Song Jin (song@iso.iso.unibas.ch) wrote:
  18. |> >: I have a question:
  19. |> >: 
  20. |> >: void myfunction(void)
  21. |> >: {
  22. |> >:  double *mypointer = new double[100];
  23. |> >: 
  24. |> >:  .....
  25. |> >: 
  26. |> >: }
  27. |> >: 
  28. |> >: The mypointer and the buffer it pointed will be auto-deleted after returned
  29. |> >: from myfunction, am I right?
  30. |> >: 
  31. |> 
  32. |> >Absolutely not. Any memory allocated with new has to be deleted 
  33. |> >explicitly. 
  34. |> 
  35. |> >    delete [] mypointer;
  36. |> 
  37. |> >Otherwise, once all pointers to it have gone out of scope, it'll just sit
  38. |> >there unreachable until the heap itself is returned to wherever it came
  39. |> >from (if that happens) or garbage collection returns it to the heap (if
  40. |> >your system has garbage collection) or until the machine is rebooted. This
  41. |> >is called a "memory leak". 
  42. |> 
  43. |> An alternative to manually deleteing the pointer is to use an
  44. |> auto_array<T> class instead of a plain function pointer. This class is
  45. |> very similar to the auto_ptr<T> class which I know is being included
  46. |> in the Standard C++ Library. I don't know if auto_array<T> is also
  47. |> part of the SC++L, but it is obviously needed since "delete []" not
  48. |> plain "delete" needs to be called.
  49. |> 
  50. |> Here is my implementation of auto_array<T>:
  51. |> 
  52. |> template <class T>
  53. |> class auto_array
  54. |> {
  55. |>  public:
  56. |>     auto_array(T* pp = 0) : p(pp) {}
  57. |>     auto_array(auto_array<T>& x) : p(x.p) {x.p = 0;}
  58. |>     ~auto_array() {delete [] p;}
  59. |> 
  60. |>     T& operator [](int i) {return p[i];}
  61. |>     auto_array<T>& operator =(T* pp) {delete [] p; p = pp; return *this;}
  62.  
  63. great code...
  64.  
  65. especially when it is used as shown below:
  66.  
  67. char* pc = new char[10];
  68. auto_array<char> ac( pc );
  69.  
  70. ...
  71.  
  72. ac = pc;
  73.  
  74. |>     auto_array<T>& operator =(auto_array<T>& x)
  75. |>         {delete [] p; p = x.p; x.p = 0; return *this;}
  76.  
  77. or 
  78.  
  79. ac = ac;
  80.  
  81.                                                   OK
  82.